package com.rpp.utils;

import com.rpp.annotation.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Description
 * @Author rpp
 * @Date 2020/6/25 11:31 下午
 */
@Component
public class ConnectionUtils {

    // 存储当前线程的连接
    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    /**
     * 从当前线程获取连接
     *
     * @return
     * @throws SQLException
     */
    public Connection getCurrentThreadConn() throws SQLException {
        Connection connection = threadLocal.get();
        if (null == connection) {
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }
}
